package com.rboat.web.controller.blog;

import com.rboat.blog.domain.BlogArticle;
import com.rboat.blog.domain.BlogArticleType;
import com.rboat.blog.service.IBlogArticleService;
import com.rboat.blog.service.IBlogArticleTypeService;
import com.rboat.common.annotation.Log;
import com.rboat.common.core.controller.BaseController;
import com.rboat.common.core.domain.AjaxResult;
import com.rboat.common.core.page.TableDataInfo;
import com.rboat.common.enums.BusinessType;
import com.rboat.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章Controller
 *
 * @author rboat
 * @date 2019-11-05
 */
@Controller
@RequestMapping("/blog/article")
public class BlogArticleController extends BaseController {
    private String prefix = "blog/article";

    @Autowired
    private IBlogArticleService blogArticleService;
    @Autowired
    private IBlogArticleTypeService blogArticleTypeService;

    @RequiresPermissions("blog:article:view")
    @GetMapping()
    public String article() {
        return prefix + "/article";
    }

    /**
     * 查询文章列表
     */
    @RequiresPermissions("blog:article:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BlogArticle blogArticle) {
        startPage();
        List<BlogArticle> list = blogArticleService.selectBlogArticleList(blogArticle);
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @RequiresPermissions("blog:article:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BlogArticle blogArticle) {
        List<BlogArticle> list = blogArticleService.selectBlogArticleList(blogArticle);
        ExcelUtil<BlogArticle> util = new ExcelUtil<BlogArticle>(BlogArticle.class);
        return util.exportExcel(list, "article");
    }

    /**
     * 新增文章
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<BlogArticleType> blogArticleTypeList = blogArticleTypeService.selectBlogArticleTypeList(new BlogArticleType());
        mmap.put("articleTypes",blogArticleTypeList);
        return prefix + "/add";
    }

    /**
     * 新增保存文章
     */
    @RequiresPermissions("blog:article:add")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BlogArticle blogArticle) {
        return toAjax(blogArticleService.insertBlogArticle(blogArticle));
    }

    /**
     * 修改文章
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        BlogArticle blogArticle = blogArticleService.selectBlogArticleById(id);
        mmap.put("blogArticle", blogArticle);
        List<BlogArticleType> blogArticleTypeList = blogArticleTypeService.selectTypeForArticleEdit(blogArticle.getArticleTypeId());
        mmap.put("articleTypes",blogArticleTypeList);
        return prefix + "/edit";
    }

    /**
     * 修改保存文章
     */
    @RequiresPermissions("blog:article:edit")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BlogArticle blogArticle) {
        return toAjax(blogArticleService.updateBlogArticle(blogArticle));
    }

    /**
     * 删除文章
     */
    @RequiresPermissions("blog:article:remove")
    @Log(title = "文章", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(blogArticleService.deleteBlogArticleByIds(ids));
    }
}
